import type { FormInstance } from 'ant-design-vue/lib/form'
import type { NamePath } from 'ant-design-vue/lib/form/interface'
import type { Ref } from 'vue'
import { unref } from 'vue'


export function useFormValid<T extends object = any>(formRef: Ref<FormInstance>) {
  const validate = () => {
    const form = unref(formRef)
    return form?.validate ?? ((_nameList?: NamePath) => Promise.resolve())
  }

  async function validForm() {
    const form = unref(formRef)
    if (!form)
      return
    const data = await form.validate()
    return data as T
  }

  return { validate, validForm }
}
